	package com.sinosoft.shopcar;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.sinosoft.base.GenericDaoHibernate;

@SuppressWarnings("unchecked")
@Repository("shopcarDao")
public class ShopcarDaoHibernate extends GenericDaoHibernate<Shopinfo, Long> implements
		ShopcarDao {

	public ShopcarDaoHibernate() {
		super(Shopinfo.class);
	}
	
	
	public List<Shopinfo> getShopcarList(String username,int first,int pagesize)
	{
		Session session =null;
		List<Shopinfo> shopcarlist = null;
		session=this.getHibernateTemplate().getSessionFactory().openSession();
		Criteria c = session.createCriteria(Shopinfo.class);
		c.setFirstResult(first);
		c.setMaxResults(pagesize);
		c.addOrder(Order.desc("orderid"));
		if(username!=null&&!username.isEmpty())
			c.add(Restrictions.eq("username","admin"));
		shopcarlist = c.list();
		session.close();
		return shopcarlist;
	}
	
	public Shopinfo saveShopcar(Shopinfo shopinfo) {
		getHibernateTemplate().saveOrUpdate(shopinfo);
		return shopinfo;
	}
	
	public void removeShopcarlist(List<Shopinfo> shoplist) {
		getHibernateTemplate().deleteAll(shoplist);
	}
	
	public List<Shopinfo> getShopinfoListByIds(String ids)
	{
		String hqls = "from Shopinfo where orderid in("+ids+")";
		List<Shopinfo> shoplist = getHibernateTemplate().find(hqls);
		return shoplist;
	}
	
	public void removeShopcar(String id)
	{
		String hqls = "from Shopinfo where id = " + id;
		List<Shopinfo> shoplist = getHibernateTemplate().find(hqls);
		removeShopcarlist(shoplist);
	}
	
	public Shopinfo getShopinfoById(int id)
	{
		String hqls = "from Shopinfo where orderid="+id;
		List<Shopinfo> shopinfolist = getHibernateTemplate().find(hqls);
		Shopinfo shopinfo = null;
		if(shopinfolist!=null && shopinfolist.size()>0)
			shopinfo = shopinfolist.get(0);
		return shopinfo;
	}
	
	public List<Shopinfo> getShopcarListByUsername(String username,String orderids)
	{
		StringBuffer hql = new StringBuffer("from Shopinfo where username='"+username+"'");
		if(orderids!=null)
		{
			String[] orderlist = orderids.split(",");
			hql.append(" and (");
			for (int i = 0; i < orderlist.length; i++) {
				if(i==0)
					hql.append(" orderid="+orderlist[i]);
				else
					hql.append(" or orderid="+orderlist[i]);
			}
			hql.append(")");
		}
		List<Shopinfo> shopinfolist = getHibernateTemplate().find(hql.toString());
		return shopinfolist;
	}
	
	public int getShopcarCount(String sql)
	{
		int pageCount = 0;
		Session session =null;
		try{
			session=this.getHibernateTemplate().getSessionFactory().openSession();
			Query query = session.createSQLQuery(sql);
		    List list= query.list();
		    pageCount = Integer.valueOf(list.get(0).toString());
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			session.close();
		} 
		return pageCount;
	}
}
